package com.xhl.doeverything.controller;

import com.xhl.doeverything.dozer.DozerUtils;
import com.xhl.doeverything.dto.UserRequest;
import com.xhl.doeverything.entity.ApiResult;
import com.xhl.doeverything.entity.PageQueryCondition;
import com.xhl.doeverything.entity.PageQueryResult;
import com.xhl.doeverything.entity.UserDO;
import com.xhl.doeverything.service.UserService;
import com.xhl.doeverything.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author xionghuilong
 * @description: 用户信息管理
 * @date 2022/1/21 14:45
 */
@RestController
@Slf4j
@Api(tags = "用户信息管理")
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private DozerUtils dozer;

    @PostMapping("queryUserList")
    @ApiOperation("用户列表查询")
    public ApiResult<List<UserVO>> queryUserList(@RequestBody UserRequest request){

        log.info("入参 -> {}",request);
        List<UserDO> userList = this.userService.queryUserList(request);
        return ApiResult.success(this.dozer.mapList(userList, UserVO.class));
    }

    @PostMapping("add")
    @ApiOperation("用户信息录入")
    public ApiResult<String> add(@RequestBody UserRequest request){

        int count =  this.userService.addUser(request);
        if(count <= 0){
            return ApiResult.fail("数据录入失败!");
        }else{
            return ApiResult.success("数据录入成功！");
        }
    }

    @PostMapping("addUserWithIndex")
    @ApiOperation("用户信息录入方式2")
    public ApiResult<String> addUserWithIndex(@RequestBody UserRequest request){

        int count =  this.userService.addUserWithIndex(request);
        if(count <= 0){
            return ApiResult.fail("数据录入失败!");
        }else{
            return ApiResult.success("数据录入成功！");
        }
    }

    @PostMapping("update")
    @ApiOperation("用户信息修改")
    public ApiResult<String> update(@RequestBody UserRequest request){
        int count =  this.userService.updateUser(request);
        return ApiResult.success("成功修改" + count + "条记录！");
    }

    @GetMapping("delete")
    @ApiOperation("用户信息删除")
    public ApiResult<String> delete(@RequestParam(value = "name") String name ){
        int count = this.userService.deleteUser(name);
        return ApiResult.success("成功删除" + count + "条记录！");
    }

    @PostMapping("queryPage")
    @ApiOperation("分页查询")
    public PageQueryResult<UserDO> queryPage(@RequestBody PageQueryCondition<UserRequest> request){
        PageQueryResult<UserDO> result = this.userService.queryPage(request);
        return result;
    }

    @PostMapping("esQueryPage")
    @ApiOperation("es分页查询")
    public PageQueryResult<UserDO> esQueryPage(@RequestBody PageQueryCondition<UserRequest> request){
        PageQueryResult<UserDO> result = this.userService.esQueryPage(request);
        return result;
    }
}
